//
//  T4ViewController.m
//  EarthIOS
//
//  Created by Xiaohui Chen on 13-7-25.
//  Copyright (c) 2013年 Team4.us. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
{
    
}
@end

@implementation BaseViewController

- (id)initWithBundle:(T4Bundle *)bundle
{
    self = [self init];
    if (self) {
        self.bundle = bundle;
    }    
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - public function

- (void)back
{
    if (self.mode == T4ControllerAppendModeNewNav
        || self.mode == T4ControllerAppendModePresent) {
        [self.navigationController dismissViewControllerAnimated:YES
                                                      completion:nil];
    } else if (self.mode == T4ControllerAppendModePush) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        NSAssert(NO, @"self.mode(%d)还未处理", self.mode);
    }
}

- (void)popLastPage
{
    [self popSeveralPage:1];
}

- (void)popSeveralPage:(NSUInteger)count
{
    if (count <= 0) {
        return;
    }
    
    NSArray *viewControllers = self.navigationController.viewControllers;
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:viewControllers];
    NSUInteger popCount = MIN(count, viewControllers.count - 1);
    
    NSUInteger lastIndex = viewControllers.count - 2;
    for (NSUInteger i=0; i<popCount; i++) {
        [array removeObjectAtIndex:lastIndex - i];
    }
    
    self.navigationController.viewControllers = array;
}

- (UIViewController *)showWithClassName:(NSString *)className
{
    return [self showWithClassName:className bundle:nil];
}

- (UIViewController *)showWithClassName:(NSString *)className
                                 bundle:(T4Bundle *)bundle
{
    return [self showWithClassName:className
                              mode:T4ControllerAppendModePush
                            bundle:bundle];
}

- (UIViewController *)showWithClassName:(NSString *)className
                                   mode:(T4PushControllerMode)mode
                                 bundle:(T4Bundle *)bundle
{
    Class c = NSClassFromString(className);
    NSAssert(c != nil, @"未找到对应的Class(%@)", className);
    NSAssert([c isSubclassOfClass:[BaseViewController class]], @"Class(%@)必须是T4ViewController的子类", className);
    BaseViewController *controller = [[c alloc] initWithBundle:bundle];
    controller.mode = mode;
    
    if (mode == T4ControllerAppendModeAdd) {
        [self.navigationController addChildViewController:controller];
        [self.navigationController.view addSubview:controller.view];
    } else if (mode == T4ControllerAppendModePresent) {
        [self.navigationController presentViewController:controller
                                                animated:YES
                                              completion:nil];
    } else if (mode == T4ControllerAppendModeNewNav) {
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
        [self.navigationController presentViewController:navigationController
                                                animated:YES
                                              completion:nil];
    } else {
        [self.navigationController pushViewController:controller
                                             animated:YES];
    }
    return controller;
}

@end
